package com.sign2024.sign2024project.capitalconstruction.controller;

import com.alibaba.excel.EasyExcel;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.entry.capitalconstruction.JjUpdateProject;
import com.sign2024.sign2024project.capitalconstruction.service.JjUpdateDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author yujie
 * @data 2024/9/13 17:28
 */
@Api(tags = "基建")
@RestController
@RequestMapping("/api/jjUpdate")
public class JjUpdateDataController {

    @Resource
    private JjUpdateDataService jjUpdateDataService;

    @Log(title = "基建", businessType = "导入")
    @ApiOperation(value = "更新抽取方案excel导入", notes = "更新抽取方案excel导入")
    @Transactional
    @PostMapping("/projectInport")
    public CommonResult<JjUpdateProject> projectInport(MultipartFile file) {

        try {
            // 获取上传文件的名称
            String fileName = file.getOriginalFilename();

            List<JjUpdateProject> list = EasyExcel.read(file.getInputStream()).head(JjUpdateProject.class).sheet().doReadSync();
            jjUpdateDataService.addExport(list, fileName);
            return CommonResult.success();

        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "基建", businessType = "更新")
    @ApiOperation(value = "更新标段对应关系数据", notes = "更新标段对应关系数据")
    @PostMapping("/save")
    public CommonResult save(@RequestParam @ApiParam("任务标识,0工程服务,1基建,2物资类") Integer taskState) {
        jjUpdateDataService.saveJJUpdate(taskState);
        return CommonResult.success();
    }
}
